<?php

namespace console\db\channels;

/**
 * Migration自定义扩展
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
abstract class AlterTableMigration extends \console\db\Migration {

    /**
     * 升级所需增加的表字段
     * @inheritdoc
     */
    public function safeUp() {
        foreach ($this->getAlterOptions() as $table => $columns) {
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            $tableColumns = $this->db->getTableSchema($tableName)->columnNames;
            if (empty($columns)) {
                continue;
            }
            foreach ($columns as $column => $type) {
                if (!in_array($column, $tableColumns)) {
                    $this->addColumn($tableName, $column, $type);
                } else {
                    $this->alterColumn($tableName, $column, $type);
                }
            }
        }
    }

    /**
     * @inheritdoc
     */
    abstract public function getAlterOptions();
}
